package com.odontosis.view.backup;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;

import com.odontosis.util.StringUtilsOdontosis;
import com.odontosis.view.OdontosisForm;

public class FormBackup extends OdontosisForm {

	private String endereco;
	
	public List<String> erros;
	
	private FormFile arquivo;

	

	public FormFile getArquivo() {
		return arquivo;
	}

	public void setArquivo(FormFile arquivo) {
		this.arquivo = arquivo;
	}

	public List<String> getErros() {
		return erros;
	}

	public void setErros(List<String> erros) {
		this.erros = erros;
	}

	public String getEndereco() {
		return endereco;
	}

	public void setEndereco(String endereco) {
		this.endereco = endereco;
	}
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		ActionMessages messages = new ActionMessages();
		if(!StringUtilsOdontosis.isVazia(getMetodo())) {
			if(getMetodo().equals("gerar")) {
				if(StringUtilsOdontosis.isVazia(getEndereco())){
					messages.add("nome", new ActionMessage("O campo Endereço deve ser informado."));
				}
			}
		}

		if (messages.size() > 0 && getMetodo() != null) {
			errors.add(messages);
			erros = new ArrayList<String>();
			for (Iterator iterator = messages.get(); iterator.hasNext();) {
				ActionMessage string = (ActionMessage) iterator.next();
				erros.add(string.getKey());
			}
			request.setAttribute("mensagens", erros);
			return errors;
		} else {
			request.setAttribute("mensagens", null);
			return super.validate(mapping, request);
		}

	}

	
}
